package info.brenstar.boxedin.collisions;

import info.brenstar.boxedin.core.Sprite;

import java.util.ArrayList;
import java.util.HashMap;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Rectangle;

public class CollisionMap {
	
	private HashMap<Integer, Sector> map;
	private int sectorWidth, sectorHeight;
	
	public CollisionMap(int sectorWidth, int sectorHeight) {
		int x=0, y=0, i=0;
		this.sectorWidth = sectorWidth;
		this.sectorHeight = sectorHeight;
		map = new HashMap<Integer, Sector>();
		
		while (y < 640) {
			map.put(i, new Sector(i, new Rectangle(x, y, sectorWidth, sectorHeight)));
			x += sectorWidth;
			if (x >= 800) {
				y += sectorHeight;
				x = 0;
			}
			i++;
			
		}
	}
	
	public void debug(Graphics g) {
		for (int i = 0; i != map.size(); i++) {
			Sector sect = map.get(i);
			if (sect != null) {
				sect.debug(g);
			}
		}
	}
	
	public boolean isSectorEmpty(int id) {
		return map.get(id).getSprites().isEmpty();
	}
	
	public int getSectorWidth() {
		return this.sectorWidth;
	}
	
	public int getSectorHeight() {
		return this.sectorHeight;
	}
	
	public int getSectorId(int x, int y) {
		int column = (int)Math.floor((double)x / sectorWidth);
		int row = (int)Math.floor((double)y / sectorHeight);
		return column + (row * (int)Math.ceil(800.0/sectorWidth));
	}
	
	public void add(Sprite sprite) {
		Sector sector = getSector(sprite);
		sector.add(sprite);
		
		/*
		for (Sector sect : map.values()) {
			if (rect.intersects(sect.getBounds())) {
				sect.add(sprite);
			}
		}*/
		
	}
	
	public void update(Sprite sprite) {
		Sector current = sprite.getSector();
		
		if (current != null) {
			int id = getSectorId((int)sprite.getX(), (int)sprite.getY());
			if (current.getID() != id) {
				current.remove(sprite);
				Sector sector = getSector(id);
				sector.add(sprite);
				sprite.setSector(sector);
				System.out.println(sector);
			}
		}
	}
	
	/**
	 * Gets the sector the sprite is located at
	 * @param sprite
	 * @return
	 */
	public Sector getSector(Sprite sprite) {
		Rectangle rect = (Rectangle)sprite.getMask();
//		int column = (int)Math.floor(rect.getX() / sectorWidth);
//		int row = (int)Math.floor(rect.getY() / sectorHeight);
		int sectorId = getSectorId((int)rect.getX(), (int)rect.getY());
		
		return map.get(sectorId);
	}
	
	public Sector getSector(int id) {
		return map.get(id);
	}
	
	public Sector[] getNeighbors(Sprite sprite) {
		Sector[] neighbors = new Sector[3];
		
		Sector spriteSector = getSector(sprite);
		Rectangle bounds = spriteSector.getBounds();
		
		if (bounds.getX() != 800 - sectorWidth) {
			neighbors[0] = map.get(spriteSector.getID() + 1);
		}
		
		if (bounds.getY() != 640 - sectorHeight) {
			neighbors[1] = map.get(spriteSector.getID() + (int)Math.ceil(800.0 / sectorWidth));
		}
		
		if (neighbors[0] != null && neighbors[1] != null) {
			neighbors[2] = map.get(spriteSector.getID() + (int)Math.ceil(800.0 / sectorWidth) + 1);
		}
		
		return neighbors;
	}
 	
	
}
